

package com.viweb.storemanager.core;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;


public class ColorCellRenderer extends JLabel implements ListCellRenderer {
    private Color oddLine = null;
    private Color evenLine = null;
    private Color selectedLine = null;
    
    public ColorCellRenderer() {
        this.oddLine = new Color(240, 240, 255);
        this.evenLine = Color.WHITE;
        this.selectedLine = new Color(172, 210, 255);
        
        this.setOpaque(true);
    }
    
    /*
     * This method finds the image and text corresponding
     * to the selected value and returns the label, set up
     * to display the text and image.
     */
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        
        if (isSelected) {
            this.setBackground(this.selectedLine);
        } else {
            if (index % 2 == 0) {
                this.setBackground(this.evenLine);
            } else {
                this.setBackground(this.oddLine);
            }
        }
        
        this.setForeground(((ColorListItem) value).getColor());
        if (value != null) {
            this.setText(" " + value.toString() + " ");
        } else {
            this.setText(null);
        }
        try {
            this.setFont(list.getFont());
        } catch (Exception ex) {
        }
        
        return this;
    }
    
    @Override
    public Dimension getPreferredSize() {
        Dimension dim = super.getPreferredSize();
        return new Dimension(dim.width, dim.height + 4);
    }
    
}